/*     */ package com.zimbra.cs.store.file;
/*     */ 
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsRequest;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsRequest.DedupAction;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsResponse;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsResponse.DedupStatus;
/*     */ import com.zimbra.soap.admin.type.IntIdAttr;
/*     */ import com.zimbra.soap.admin.type.VolumeIdAndProgress;
/*     */ import java.io.PrintStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BlobDeduperUtil
/*     */ {
/*     */   private static final String LO_HELP = "help";
/*     */   private static final String LO_VERBOSE = "verbose";
/*     */   private static final String LO_VOLUMES = "volumes";
/*     */   private Options options;
/*  47 */   private boolean verbose = false;
/*  48 */   private List<Short> volumeIds = new ArrayList();
/*     */   private DedupeBlobsRequest.DedupAction action;
/*     */   
/*     */   private BlobDeduperUtil() {
/*  52 */     this.options = new Options();
/*     */     
/*  54 */     this.options.addOption(new Option("h", "help", false, "Display this help message."));
/*  55 */     this.options.addOption(new Option("v", "verbose", false, "Display stack trace on error."));
/*     */     
/*  57 */     Option o = new Option(null, "volumes", true, "Specify which volumes to dedupe.  If not specified, dedupe all volumes.");
/*  58 */     o.setArgName("volume-ids");
/*  59 */     this.options.addOption(o);
/*     */   }
/*     */   
/*     */   private void usage(String errorMsg) {
/*  63 */     int exitStatus = 0;
/*     */     
/*  65 */     if (errorMsg != null) {
/*  66 */       System.err.println(errorMsg);
/*  67 */       exitStatus = 1;
/*     */     }
/*  69 */     HelpFormatter format = new HelpFormatter();
/*  70 */     format.printHelp(new PrintWriter(System.err, true), 80, "zmdedupe [options] start/status/stop", null, this.options, 2, 2, "\nThe \"start/stop\" command is required, to avoid unintentionally running a blob dedupe.  Id values are separated by commas.");
/*     */     
/*     */ 
/*     */ 
/*  74 */     System.exit(exitStatus);
/*     */   }
/*     */   
/*     */   private void parseArgs(String[] args) throws ParseException
/*     */   {
/*  79 */     GnuParser parser = new GnuParser();
/*  80 */     CommandLine cl = parser.parse(this.options, args);
/*     */     
/*  82 */     if (CliUtil.hasOption(cl, "help")) {
/*  83 */       usage(null);
/*     */     }
/*     */     
/*     */ 
/*  87 */     if (cl.getArgs().length == 0) {
/*  88 */       usage(null);
/*  89 */     } else { if (cl.getArgs()[0].equals("stop")) {
/*  90 */         this.action = DedupeBlobsRequest.DedupAction.stop;
/*  91 */         return; }
/*  92 */       if (cl.getArgs()[0].equals("status")) {
/*  93 */         this.action = DedupeBlobsRequest.DedupAction.status;
/*  94 */         return; }
/*  95 */       if (cl.getArgs()[0].equals("start")) {
/*  96 */         this.action = DedupeBlobsRequest.DedupAction.start;
/*  97 */       } else if (cl.getArgs()[0].equals("reset")) {
/*  98 */         if (CliUtil.confirm("This will remove all the metadata used by dedupe process. Continue?")) {
/*  99 */           this.action = DedupeBlobsRequest.DedupAction.reset;
/*     */         } else {
/* 101 */           System.exit(0);
/*     */         }
/*     */       } else {
/* 104 */         usage(null);
/*     */       }
/*     */     }
/* 107 */     this.verbose = CliUtil.hasOption(cl, "verbose");
/*     */     
/* 109 */     String volumeList = CliUtil.getOptionValue(cl, "volumes");
/* 110 */     if (volumeList != null) {
/* 111 */       for (String id : volumeList.split(",")) {
/*     */         try {
/* 113 */           this.volumeIds.add(Short.valueOf(Short.parseShort(id)));
/*     */         } catch (NumberFormatException e) {
/* 115 */           usage("Invalid volume id: " + id);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void run() throws Exception {
/* 122 */     CliUtil.toolSetup();
/* 123 */     SoapProvisioning prov = SoapProvisioning.getAdminInstance();
/* 124 */     prov.soapZimbraAdminAuthenticate();
/* 125 */     DedupeBlobsRequest request = new DedupeBlobsRequest(this.action);
/* 126 */     for (Iterator i$ = this.volumeIds.iterator(); i$.hasNext();) { short volumeId = ((Short)i$.next()).shortValue();
/* 127 */       request.addVolume(new IntIdAttr(volumeId));
/*     */     }
/* 129 */     Element respElem = prov.invoke(JaxbUtil.jaxbToElement(request));
/* 130 */     DedupeBlobsResponse response = (DedupeBlobsResponse)JaxbUtil.elementToJaxb(respElem);
/* 131 */     if (this.action == DedupeBlobsRequest.DedupAction.start) {
/* 132 */       System.out.println("Dedupe scheduled. Run \"zmdedupe status\" to check the status.");
/*     */     } else {
/* 134 */       System.out.println("Status = " + response.getStatus().name());
/* 135 */       System.out.println("Total links created = " + response.getTotalCount());
/* 136 */       System.out.println("Total size saved = " + response.getTotalSize());
/* 137 */       VolumeIdAndProgress[] volumeBlobsProgress = response.getVolumeBlobsProgress();
/* 138 */       if ((volumeBlobsProgress != null) && (volumeBlobsProgress.length > 0)) {
/* 139 */         System.out.printf("%32s : %10s - %s\n", new Object[] { "Groups populated in volume blobs", "volumeId", "groups/total_groups" });
/* 140 */         for (VolumeIdAndProgress idAndProgress : volumeBlobsProgress) {
/* 141 */           System.out.printf("%32s   %10s - %s\n", new Object[] { "", idAndProgress.getVolumeId(), idAndProgress.getProgress() });
/*     */         }
/*     */       }
/* 144 */       VolumeIdAndProgress[] blobDigestsProgress = response.getBlobDigestsProgress();
/* 145 */       if ((blobDigestsProgress != null) && (blobDigestsProgress.length > 0)) {
/* 146 */         System.out.printf("%32s : %10s - %s\n", new Object[] { "Digests Processed", "volumeId", "digests/total_digests" });
/* 147 */         for (VolumeIdAndProgress idAndProgress : blobDigestsProgress) {
/* 148 */           System.out.printf("%32s   %10s - %s\n", new Object[] { "", idAndProgress.getVolumeId(), idAndProgress.getProgress() });
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 155 */     BlobDeduperUtil app = new BlobDeduperUtil();
/*     */     try
/*     */     {
/* 158 */       app.parseArgs(args);
/*     */     } catch (ParseException e) {
/* 160 */       app.usage(e.getMessage());
/*     */     }
/*     */     try
/*     */     {
/* 164 */       app.run();
/*     */     } catch (Exception e) {
/* 166 */       if (app.verbose) {
/* 167 */         e.printStackTrace(new PrintWriter(System.err, true));
/*     */       } else {
/* 169 */         String msg = e.getMessage();
/* 170 */         if (msg == null) {
/* 171 */           msg = e.toString();
/*     */         }
/* 173 */         System.err.println(msg);
/*     */       }
/* 175 */       System.exit(1);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/store/file/BlobDeduperUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */